﻿using WRJClient.Model;
using WRJClient.ViewModel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace WRJClient.Common
{
    public sealed class MissionManager : DataManager<MissionViewModel>
    {
        private static readonly MissionManager _instance = new MissionManager();
        public static MissionManager Instance { get { return _instance; } }
        public int Count { get { return _datas.Count(); } }
        public int UnFinishedCount
        {
            get
            {
               return _datas.Count(m => m.IsFinished == false);
            }
        }
        public int ActionCount
        {
            get
            {
                return _datas.Count(m => m.IsAction == true);
            }
        }


        private MissionManager() { }

        protected override bool addChild(MissionViewModel t)
        {
            var vm = _datas.FirstOrDefault(m => m.StaticData.DbId == t.StaticData.DbId);
            if (vm == null)
            {
                _datas.Add(t);
                t.PropertyChanged += T_PropertyChanged;
                OnPropertyChanged("UnFinishedCount");
                return true;
            }
            return false;
        }

        protected override bool removeChild(MissionViewModel t)
        {
            var vm = _datas.FirstOrDefault(m => m.StaticData.DbId == t.StaticData.DbId);
            if (vm != null)
            {
                _datas.Remove(vm);
                t.PropertyChanged += T_PropertyChanged;
                OnPropertyChanged("UnFinishedCount");
                return true;
            }
            return false;
        }

        private void T_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            if (e.PropertyName.CompareTo("IsAction") == 0)
            {
                OnPropertyChanged("ActionCount");
            }
            else if (e.PropertyName.CompareTo("IsFinished") == 0)
            {
                OnPropertyChanged("UnFinishedCount");
            }
        }

        public void UpdateMission(string dbId, MissionModel model)
        {
            MissionViewModel vm = Get(dbId);
            if (vm != null && model != null)
            {
                if (vm.StaticData.DbId == model.DbId)
                {
                    vm.SetData(model);
                }
            }
        }

        public override MissionViewModel Get(string dbId)
        {
            return _datas.FirstOrDefault(m => m.StaticData.DbId == dbId);
        }
        public MissionViewModel GetByUavId(string uavId)
        {
            return _datas.FirstOrDefault(m => m.StaticData.UavId == uavId);
        }
        public MissionViewModel GetCurrentByUavId(string uavId)
        {
            return _datas.FirstOrDefault(m => m.StaticData.UavId == uavId && m.StateName == "执行中");
        }
        public MissionViewModel GetByAirlineDbId(string airlineDbId)
        {
            return _datas.FirstOrDefault(m => m.StaticData.AirlineId == airlineDbId);
        }


    }
}
